package com.cloud.autoconfigure;

import com.cloud.async.business.context.AbstractApplicationContext;
import com.cloud.async.business.context.support.StandardAsyncContext;
import com.cloud.async.result.core.factory.StandardResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 自动配置类
 *
 * @author chengrui
 */
@Configuration
@EnableConfigurationProperties(WebAsyncAutoBean.class)
@ConditionalOnBean({WebAsyncMarker.class, Executor.class})
public class WebAsyncAutoConfiguration {

    @Autowired
    private Executor executor;

    @Bean
    @ConditionalOnMissingBean(AbstractApplicationContext.class)
    StandardAsyncContext simpleBean(WebAsyncAutoBean webAsyncAutoBean) {
        return new StandardAsyncContext(executor,
                new StandardResultFactory(),
                webAsyncAutoBean.getMilliSecond(),
                webAsyncAutoBean.getDataCapacity(),
                webAsyncAutoBean.getDataLoadFactor(),
                webAsyncAutoBean.getDataConcurrencyLevel());
    }


}
